/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.order.api.entity.OrderDelivery;
import com.pig4cloud.pigx.order.service.OrderDeliveryService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单物流
 *
 * @author pigx code generator
 * @date 2023-12-16 19:16:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/order/delivery")
@Tag(description = "order_delivery", name = "订单物流管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OrderDeliveryController {

	private final OrderDeliveryService orderDeliveryService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param orderDelivery 订单物流
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	public R getOrderDeliveryPage(Page page, OrderDelivery orderDelivery) {
		return R.ok(orderDeliveryService.page(page, Wrappers.query(orderDelivery)));
	}

	/**
	 * 通过id查询订单物流
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(orderDeliveryService.getById(id));
	}

	/**
	 * 新增订单物流
	 * @param orderDelivery 订单物流
	 * @return R
	 */
	@Operation(summary = "新增订单物流", description = "新增订单物流")
	@SysLog("新增订单物流")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('order_delivery_add')")
	public R save(@RequestBody OrderDelivery orderDelivery) {
		return R.ok(orderDeliveryService.addOrderDelivery(orderDelivery));
	}

	/**
	 * 修改订单物流
	 * @param orderDelivery 订单物流
	 * @return R
	 */
	@Operation(summary = "修改订单物流", description = "修改订单物流")
	@SysLog("修改订单物流")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('order_delivery_add')")
	public R updateById(@RequestBody OrderDelivery orderDelivery) {
		return R.ok(orderDeliveryService.updateById(orderDelivery));
	}

	/**
	 * 通过id删除订单物流
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除订单物流", description = "通过id删除订单物流")
	@SysLog("通过id删除订单物流")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('order_delivery_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(orderDeliveryService.removeById(id));
	}

	/**
	 * 导出excel 表格
	 * @param orderDelivery 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('order_delivery_export')")
	public List<OrderDelivery> export(OrderDelivery orderDelivery) {
		return orderDeliveryService.list(Wrappers.query(orderDelivery));
	}

	/**
	 * 根据类型获取订单物流信息
	 * @return
	 */
	@GetMapping("getOrderTypeDelivery")
	public R getOrderTypeDelivery(OrderDelivery orderDelivery) {
		LambdaQueryWrapper<OrderDelivery> lqwOrderDelivery = new LambdaQueryWrapper<>();
		lqwOrderDelivery.eq(OrderDelivery::getOrderId, orderDelivery.getOrderId());
		lqwOrderDelivery.eq(OrderDelivery::getDeliveryOrderType, orderDelivery.getDeliveryOrderType());
		return R.ok(orderDeliveryService.getOne(lqwOrderDelivery, false));
	}

}
